import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  // 首页
  {
    path: '/msite',
    name: 'Msite',
    component: () => import('../views/Msite/Msite')
  },
  // 详情页
  {
    path: '/details',
    name: 'Details',
    component: () => import('../views/Msite/Details')
  },
  // 车辆搜索
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Msite/Search')
  },
  // 租车GPS
  {
    path: '/rental',
    name: 'Rental',
    component: () => import('../views/Msite/Rental')
  },
  // 收藏
  {
    path: '/collection',
    name: 'Collection',
    component: () => import('../views/Collection/Collection')
  },
  // 订单
  {
    path: '/order',
    name: 'Order',
    component: () => import('../views/Order/Order')
  },
  // 我的
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile/Profile')
  },
  // 登录
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  },
  // 登录
  {
    path: '/loginphone',
    name: 'LoginPhone',
    component: () => import('../views/LoginPhone')
  },
  // 注册
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register')
  },
  // {
  //   path: '/walletdetail?title=',
  //   name: 'Walletdetail',
  //   component: () => import('../views/Profile/Details')
  // }
  // 钱包详情页
  {
    path: '/walletdetail',
    name: 'Walletdetail',
    component: () => import('../views/Profile/Details')
  },
  // 、功能
  {
    path: '/function',
    name: 'Function',
    component: () => import('../views/Profile/Function')
  },
  // 设置
  {
    path: '/setting',
    name: 'Setting',
    component: () => import('../views/Profile/Setting'),
    children: [
      {
        path: '/setting/settingdetails',
        name: 'SettingDetails',
        component: () => import('../views/Profile/SettingDetails')
      }
    ]
  },
  // 消息通知
  {
    path: '/message',
    name: 'Message',
    component: () => import('../views/Profile/Message')
  }
]
const router = new Router({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes
});

// 路由守卫
// 再配置文件中设置路由守卫
// 必须在router赋值后才能用
// beforeEach: 路由的全局配置 守卫，可以理解成中间件，拦截器，会在任意路由发生切换操作，自动触发
// router.beforeEach((to, from, next) => {
//   /**
//    *  固定3个参数
//    * to: 要跳转到哪
//    * from: 现在在哪
//    * next: 是否允许跳转
//    */
//   if(to.path === '/login') {
//     next() // 放行操作：必须调用，路由才能继续运行
//   }else {
//     next('/login')
//   }
// })

export default router;